package com.qst.financing.service.serviceImpl;

import com.alipay.api.internal.util.StringUtils;
import com.qst.financing.mapper.FinanceMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.financing.mapper.FinanceMapper;
import com.qst.financing.pojo.Finance;
import com.qst.financing.pojo.Intention;
import com.qst.financing.pojo.Recommender;
import com.qst.financing.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {
    @Autowired
    FinanceMapper financeMapper;
    @Override
    public List<Intention> selectIntentionByName(String name) {
        List<Intention> intentionList = financeMapper.selectIntentionByName(name);
        return intentionList;
    }

    @Override
    public void insertIntentionByName(Intention intention) {
        intention.setCreateTime(new Date());
        intention.setUpdateTime(new Date());
        financeMapper.insertIntentionByName(intention);
    }

    @Override
    public void updateIntentionByName(Intention intention) {
        intention.setUpdateTime(new Date());
        financeMapper.updateIntentionByName(intention);
    }

    @Override
    public void deleteIntentionByName(String name) {
        financeMapper.deleteIntentionByName(name);
    }

    @Override
    public List<Recommender> selectRecommend(String name) {
        Intention intention = financeMapper.selectIntention(name);
        if (StringUtils.isEmpty(String.valueOf(intention))) {//无意向
            System.out.println("无意向");
            List<Recommender> list = financeMapper.selectWithNoneIntention(name);
            System.out.println(list);
            return list;
        } else {//有意向
            System.out.println("有意向");
            List<Recommender> list = financeMapper.selectAllRecommend(name);
            System.out.println(list);
            return list;
        }
    }

    //PageInfo用于封装分页查询。
    @Override
    public PageInfo<Recommender> selectRecommendFY(Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Recommender> orderRecommenders = financeMapper.selectRecommendFY();
        PageInfo<Recommender> pageInfo = new PageInfo<>(orderRecommenders);
//        System.out.println(pageInfo);
        return pageInfo;
    }

    @Override
    public PageInfo<Finance> selectByFinance(Finance finance, Integer pageNum) {
        PageHelper.startPage(pageNum,20);
        List<Finance> finances = financeMapper.selectByFinance(finance);

        PageInfo<Finance> financePageInfo = new PageInfo<>(finances);
        return financePageInfo;
    }

    @Override
    public Finance selectByFinanceId(Integer id){

        Finance finance = financeMapper.selectByPrimaryKey(id);
        String[] s = finance.getFileInfo().split(" ");
        finance.setFile(s);
        return finance;
    }

    @Override
    public void updateByFinance(Finance finance){
        financeMapper.updateByPrimaryKeySelective(finance);
    }
}
